<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\admin\model\SupplierModel;
use app\admin\validate\SupplierValidate;
class Supplier extends Controller
{
    //供应商列表
    public function index()
    {

    }

    //供应商新增
    public function add()
    {
        $data['name'] =request()->param('name');//供应商名称
        $data['contact'] =request()->param('contact');//联系负责人
        $data['discount'] =request()->param('discount');//折扣
        $data['phone'] =request()->param('phone');//手机
        $data['tel'] =request()->param('tel');//电话
        $data['email'] =request()->param('email');//邮箱
        $data['fax'] =request()->param('fax');//传真
        $data['website'] =request()->param('website');//网址
        $data['region'] =request()->param('region');//地区
        $data['address'] =request()->param('address');//详细地址
        $data['bank'] =request()->param('bank');//银行名称
        $data['bank_account'] =request()->param('bank_account');//银行卡号
        $data['status'] =request()->param('status');//状态
        $data['create_time'] =time();//添加时间
        $validate =new SupplierValidate();
        if (!$validate->check($data)) {
            return json(['code'=>404,'msg'=>$validate->getError()]);
        }
        $model =new SupplierModel();
        $arr =$model->getAdd($data);
        if(!empty($arr)){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败']);
        }
    }

    //上传图片
    public function upload()
    {
        $file = request()->file('images');
        if($file){
            $savePath =ROOT_PATH .'/Public/upload/case/';
            if(!is_dir($savePath)){
                mkdir($savePath,0777,true);
            }
            $info = $file->validate(['size'=>15678,'ext'=>'jpg,jpeg,png,gif'])->move($savePath);
            if($info){
                $filename =$info->getSaveName();
                $filepath ='/Public/upload/case/'.$filename;
                return $filepath;
            }else{
                echo $file->getError();
            }
        }else{
            $this->error("请选择上传的文件");
        }
    }
}
